package com.beta.cust.bind.datasync.service.wechat;

import com.beta.cat.service.RedissonService;
import com.beta.cust.bind.datasync.constant.RedisConstant;
import com.beta.cust.bind.datasync.constant.ResultCode;
import com.beta.cust.bind.datasync.service.ISyncWechatWorkEmployeeInfo;
import com.beta.cust.bind.datasync.service.impl.SyncWechatWorkDepartmentImpl;
import com.beta.cust.bind.datasync.service.impl.SyncWechatWorkCorpInfoImpl;
import com.beta.cust.bind.datasync.service.impl.SyncWechatWorkCustInfoImpl;
import com.beta.elephant.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author：chenlei
 * @Date：2024/3/13 15:31
 */

@Service
@Slf4j
public class SyncWechatInfo {

    @Autowired
    private RedissonService redissonService;

    @Autowired
    private SyncWechatWorkCorpInfoImpl syncWechatWorkCorp;

    @Autowired
    private SyncWechatWorkDepartmentImpl syncWechatWorkDepartment;

    @Autowired
    private ISyncWechatWorkEmployeeInfo syncWechatWorkEmployeeInfo;
    @Autowired
    private SyncWechatWorkCustInfoImpl syncWechatWorkCustInfo;

    /**
     * 企微同步批量总流程
     *
     * @param corpId 企微上的企业id
     * @return
     */
    public ResultVO syncWechatInfoBatch(String corpId) {
        String lockKey = String.format(RedisConstant.SYNC_DEPTS_EMPS_LOCK_KEY, corpId);
        try {
            if (redissonService.tryLock(lockKey, RedisConstant.WAIT_TIME_ZERO)) {//一直锁着不释放，直到整个结束
                //1.同步所有的下游企业
                dealAuthCorpInfo(corpId);
                //2.同步部门
                dealDepartment(corpId);
                //3.同步员工
                dealEmployee(corpId);
                //4.同步客户信息
                dealCust(corpId);
            } else {
                log.warn("syncWechatInfo_task 同步企微用户当前已有定时任务运行中...");
                return ResultVO.success();
            }
        } catch (Exception e) {
            log.error("syncWechatInfo_task_error：{}", e.getMessage(), e);
            return ResultVO.failure(ResultCode.ERROR_CODE_1105);
        } finally {
            log.info("企微信息同步任务执行完成！");
            redissonService.unlock(lockKey);
        }
        return ResultVO.success();
    }


    private void dealAuthCorpInfo(String corpId) {
        try {
            log.info("-------------------------------调用企业微信接口同步上下游企业信息start-----------------------");
            long startTime = System.currentTimeMillis();
            //调用企业微信同步下游企业信息
            syncWechatWorkCorp.syncWechatWorkCorp(corpId);
            long endTime = System.currentTimeMillis();
            log.info("-------------------------------调用企业微信接口同步上下游企业信息end,耗时 [" + (endTime - startTime) + "]ms-----------------------");
        } catch (Exception e) {
            log.info("同步下游企业信息失败:{}", e.getMessage(), e);
        }
    }

    private void dealDepartment(String corpId) {
        try {
            log.info("-------------------------------调用企业微信接口同步部门信息start-----------------------");
            long startTime = System.currentTimeMillis();
            //调用企业微信同步部门信息
            syncWechatWorkDepartment.syncWechatWorkDepartment(corpId);
            long endTime = System.currentTimeMillis();
            log.info("-------------------------------调用企业微信接口同步部门信息end,耗时 [" + (endTime - startTime) + "]ms-----------------------");
        } catch (Exception e) {
            log.info("同步下游企业信息失败:{}", e.getMessage(), e);
        }
    }

    private void dealEmployee(String corpId) {
        try {
            log.info("-------------------------------调用企业微信接口同步员工信息start-----------------------");
            long startTime = System.currentTimeMillis();
            // 调用企业微信同步员工信息
            syncWechatWorkEmployeeInfo.syncWechatWorkEmployeeInfo(corpId);
            long endTime = System.currentTimeMillis();
            log.info("-------------------------------调用企业微信接口同步员工信息end，耗时 [" + (endTime - startTime) + "]ms-----------------------");
        } catch (Exception e) {
            log.info("同步员工信息失败:{}", e.getMessage(), e);
        }
    }

    /**
     * 处理客户信息 入口
     *
     * @param corpId
     */
    private void dealCust(String corpId) {
        try {
            log.info("-------------------------------调用企业微信接口同步客户信息 start-----------------------");
            long startTime = System.currentTimeMillis();
            //同步客户信息
            syncWechatWorkCustInfo.syncCustInfo(corpId);
            long endTime = System.currentTimeMillis();
            log.info("-------------------------------调用企业微信接口同步客户信息end，耗时 [" + (endTime - startTime) + "]ms-----------------------");
        } catch (Exception e) {
            log.info("同步客户信息失败了，失败信息是：{}", e.getMessage(), e);
        }
    }

}
